#! /usr/bin/env python
# -*- coding: utf-8 -*-
"""MOSC data format

Copyright (C) 2008-2014 Christian T. Steigies <steigies@physik.uni-kiel.de>

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.

This module offers functions to read NM data in ASCII format as used in MOSC.
"""

__author__ = "Christian T. Steigies <steigies@physik.uni-kiel.de>"
__license__ = "GPL License"

## --------------------------------------------------------------------------
import os

from nmdb.tools.datetool import valid_date
from nmdb.station.read_cosray import read_1m, read_1h

mosc_station = ["mosc"]

## --------------------------------------------------------------------------
def read_rt(options, station, data_type, year, month, day):
    """read realtime data

    options     verbose, dryrun, ...
    station     station short name, must be valid station
    data_type   "1m", "1h", "env", "meta"
    year        integer (1950-9999), may not be None
    month       integer (1-12), if None, read month 1-12
    day         integer (1-31), if None, read day 1-31

    return list of values in nmdb format
    """

    if station in mosc_station:
        pass
    else:
        raise ValueError  # wrong station

    value = []
    if valid_date(year, month, day):
        value = read_mosc(options, station, data_type, year, month, day)

    return value

## --------------------------------------------------------------------------
def read_rev(options, station, data_type, year, month, day):
    """read revised data

    options     verbose, dryrun, ...
    station     station short name, must be valid station
    data_type   "1m", "1h", "env", "meta"
    year        integer (1950-9999), may not be None
    month       integer (1-12), if None, read month 1-12
    day         integer (1-31), if None, read day 1-31

    return list of values in nmdb format
    """

    if station in mosc_station:
        pass
    else:
        raise ValueError  # wrong station

    value = []
    if valid_date(year, month, day):
        # TODO revised data in SQLite or separate database?
        value = read_mosc(options, station, data_type, year, month, day)

    return value

## --------------------------------------------------------------------------
def read_mosc(options, station, data_type, year, month, day):
    # "/home/kermes/steigies/.dosemu/drives/c/Inetpub/ftproot/CosRay!"
    #data_path = '~/data/cosray'
    #data_path = os.path.expanduser(data_path)

    pressure = [950., 1050.]  # 982 - 1028
    countrate = [50., 1000.]

    if options.verbose > 1:
        print "read_mosc:", year, month, day

    useftp = False
    #useftp = True
    if useftp:  # read directly from FTP server
#        ftpserver="ftp://cr0.izmiran.rssi.ru/COSRAY%21"
        ftpserver="ftp://cr0.izmiran.rssi.ru/COSRAY!"
        #ftpserver="ftp://cr0.izmiran.rssi.ru/COSRAY!/FTP_NM1/C/2014C/2014mosc/1409mosc.w1c"
    else:  # read from local copy
        ftpserver = os.environ["HOME"]+"/data/cosray"

    data_path = ftpserver
#    (file_c, file_p, file_u) = cosray_names(data_path, data_type, station, year, month)

#    if options.verbose > 1:
#        print file_c
#        print file_p
#        print file_u

#    useftp = data_path[0:3] == "ftp"
#    return None

    if data_type in ["1h"]:
        value = read_1h(data_path, station, pressure, countrate, year, month, day)
    elif data_type in ["1m", "ori"]:
        value = read_1m(data_path, station, pressure, countrate, year, month, day)
    elif data_type in ["env", "meta"]:
        raise ValueError  # mode not yet implemented
    else:
        raise ValueError  # illegal data_type

    return value

## --------------------------------------------------------------------------
def main():
    from nmdb.tools.configtool import parse_options

    print "This is the MOSC_ASCII module for NMDB."
    (options, args) = parse_options(mode="revise")

    station = "mosc"
    data_type = "1m"
#    data_type = "1h"

    year = 2001
    month = 1
    day = 1
    
    #data = read_rev(options, station, data_type, year, month, day)
    data = read_rt(options, station, data_type, year, month, day)
    for line in data:
        print line

## --------------------------------------------------------------------------
if __name__ == '__main__':
    main()
